package com.qf.xiaomi.dao.impl;

import com.qf.xiaomi.dao.GoodsTypeDao;
import com.qf.xiaomi.pojo.GoodsType;
import com.qf.xiaomi.utils.DataSourceUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @author 千锋教育
 * @Company http://www.mobiletrain.org/
 * @Version 1.0
 */
public class GoodsTypeDaoImpl implements GoodsTypeDao {
    @Override
    public List<GoodsType> getGoodsTypeL1() {
        QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
        String sql = "select * from tb_goods_type where level = 1";
        try {
            List<GoodsType> goodsTypeList = qr.query(sql, new BeanListHandler<>(GoodsType.class));
            return goodsTypeList;
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("查询商品分类失败");
        }


        return null;
    }

    @Override
    public GoodsType findById(int typeid) {
        QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
        String sql = "select * from tb_goods_type where  id = ?";
        try {
            GoodsType type = qr.query(sql, new BeanHandler<>(GoodsType.class), typeid);
            return type;
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("查询商品分类失败");
        }


        return null;
    }
}
